from rest_framework.views import exception_handler
from rest_framework.response import Response
from django.db import DatabaseError

def custom_exception_handler(exc, context):

    #1,判断是否是APIException的异常
    response = exception_handler(exc, context)

    #2,判断异常对象
    if response is not None:
        response.data['status_code'] = response.status_code
    else:
        if isinstance(exc,DatabaseError):
            response = Response("数据库异常了",status=500)
        else:
            response = Response("其他异常",status=500)

    return response